package com.neuedu.his.controller;


import com.neuedu.his.entity.Role;
import com.neuedu.his.entity.RoleMenu;
import com.neuedu.his.entity.User;
import com.neuedu.his.entity.UserRole;
import com.neuedu.his.framework.PageInfo;
import com.neuedu.his.framework.annotation.MethodAlias;
import com.neuedu.his.framework.servlet.BaseServlet;
import com.neuedu.his.service.RoleService;
import com.neuedu.his.service.UserService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * 项目：      his
 * 类名:       UserController
 * 创建时间：  2020/12/22  11:05
 * 描述 :
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
@WebServlet(name="RoleController" ,urlPatterns = "/role")
public class RoleController extends BaseServlet {

    private RoleService roleService = new RoleService();



    @MethodAlias("query")
    public void list(HttpServletRequest request, HttpServletResponse response) throws IOException {

        PageInfo pageInfo = roleService.queryListByPage(request);
        //将结果以json格式返回
        this.success(response, pageInfo);
    }


    public void listAll(HttpServletRequest request, HttpServletResponse response) throws IOException {

        List<Role> roleList = roleService.queryList(request);
        //将结果以json格式返回
        this.success(response, roleList);
    }

  //http://127.0.0.1:8080/his/user?type=add
    public void save(HttpServletRequest request, HttpServletResponse response) throws ParseException, NoSuchFieldException, IllegalAccessException, InstantiationException, IOException {
        System.out.println("add");

        Role user = parseParameter(request, Role.class);

        boolean success = false;
        if (user.getRoleId()!= null) { //修改
            success = roleService.update(user);
        } else { //添加
            success = roleService.save(user);
        }

        super.success(response, success);
    }

    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ParseException, NoSuchFieldException, IllegalAccessException, InstantiationException, IOException {

        String roleId = request.getParameter("roleId");

        Role role  = roleService.queryById(Integer.parseInt(roleId));

        super.success(response, role);
    }


    public void del(HttpServletRequest request, HttpServletResponse response) throws ParseException, NoSuchFieldException, IllegalAccessException, InstantiationException, IOException {

        String roleId = request.getParameter("roleId");

       boolean success = roleService.deleteById(Integer.parseInt(roleId));

        super.success(response, success);
    }



    //user?type=grantRole
    public void grantMenu(HttpServletRequest request, HttpServletResponse response) throws ParseException, NoSuchFieldException, IllegalAccessException, InstantiationException, IOException {
        boolean success = roleService.grantRole(request);

        super.success(response, success);
    }
    //user?type=roleMenus&roleId=1
    public void roleMenus(HttpServletRequest request, HttpServletResponse response) throws ParseException, NoSuchFieldException, IllegalAccessException, InstantiationException, IOException {
        List<RoleMenu> roleMenus = roleService.getRoleMenus(request);

        super.success(response, roleMenus);
    }



}